package com.zhanglinwei.openApiServer.interceptor;

import com.zhanglinwei.openApiServer.context.OpenApiRequestContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.zhanglinwei.openApiClient.constants.OpenApiConstant.*;

@Component
public class OpenApiInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        OpenApiRequestContext.put(APP_KEY_HEADER_KEY, request.getHeader(APP_KEY_HEADER_KEY));
        OpenApiRequestContext.put(SIGN_HEADER_KEY, request.getHeader(SIGN_HEADER_KEY));
        OpenApiRequestContext.put(TIMESTAMP_HEADER_KEY, request.getHeader(TIMESTAMP_HEADER_KEY));

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        OpenApiRequestContext.clear();
    }
}
